package com.schoolCommunity.community.common;

import cn.hutool.core.util.StrUtil;
import com.schoolCommunity.community.entity.common.Result;
import com.schoolCommunity.community.utils.JwtUtil;
import io.jsonwebtoken.Claims;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (StrUtil.isBlank(token)) {
            return false;
        }

        String userid;
        try {
            Claims claims
                    = JwtUtil.parseJWT(token);
            //获取存储在claims中的用户信息
            userid = claims.getSubject();
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        // 有用户，则放行
        return true;
    }
}
